/**
 * 系统初始化读取的
 * @type {*|exports|module.exports}
 */
var config = require('./config.js');
var AV = require('leanengine');
var async = require('async');
var log4js = require('log4js');
var logger = log4js.getLogger('normal');
var initInfo = AV.Object.extend('Config');
var activity = AV.Object.extend('Activity');
/*exports.getInit = function() {
    console.log('My name is Lemmy Kilmister');
};*/
global.initData = {};
exports.init = function () {
    var promise = new AV.Promise();
    global.initData.level={};
    var query = new AV.Query(initInfo);
    query.equalTo('key','level');
    query.first().then(function(res){
        global.initData.level = res.get('info');
        promise.resolve({code:1,level:global.initData.level});
    }).catch(function(err){
        logger.error(err);
        promise.reject({code:0,level:{}});
    });
    return promise;
};
exports.activity = function(){
    var promise = new AV.Promise();
    global.initData.activity = {};
    var query = new AV.Query(activity);
    query.equalTo('status','1');
    //活动结束时间大于当前时间
    //将普通时间转换为时间戳，进行比较
    query.greaterThanOrEqualTo('endDate',Math.round(new Date().getTime()/1000));
    query.find().then(function(res){
        if(!res || res.length<1 || res==='undefined'){
            global.initData.activity = {};
        }else{
            var actInfo = new Array();
            async.eachSeries(res,function(act,callback){
                actInfo.push(act);//JSON.stringify
                callback(null, actInfo);
            },function(){
                global.initData.activity = actInfo;
                //console.log(global.initData.activity);
                promise.resolve({code:1,activity:global.initData.activity});
            });
        }
    }).catch(function(err){
        global.initData.activity = {};
        logger.error(err);
        promise.reject({code:0,activity:{}});
    });
    return promise;
}
//分类控制订单等情况
exports.limitClassify = function () {
    var promise = new AV.Promise();
    global.initData.limitClassify={};
    var query = new AV.Query(initInfo);
    query.equalTo('key','limitClassify');
    query.first().then(function(res){

        global.initData.limitClassify = res.get('info');
        promise.resolve({code:1,limitClassify:global.initData.limitClassify});
    }).catch(function(err){
        logger.error(err);
        promise.reject({code:0,limitClassify:{}});
    });
    return promise;
};